package middleware

import (
	"github.com/goravel/framework/contracts/http"
	"github.com/goravel/framework/facades"
	"github.com/goravel/framework/support/carbon"
	"goravel/app/models"
)

func OvertimeMiddleware() http.Middleware {
	return func(ctx http.Context) {
		//获取user
		var user models.User
		facades.Auth().User(ctx, &user)
		if user.IsAdmin() {
			ctx.Request().Next()
			return
		}
		expireTime := user.ExpireTime
		//比较时间：如果过期时间小于当前时间，carbon
		carbon.ParseByFormat("2006-01-02 15:04:05", expireTime.String())
		if carbon.Now().Gt(carbon.Parse(expireTime.String())) {
			ctx.Request().AbortWithStatusJson(http.StatusUnauthorized, http.Json{
				"message": "用户已到期，请充值后重试",
			})
			return
		}
		ctx.Request().Next()
	}
}
